<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Moodle guest book block..
*
* @package block_moodlebook
* @copyright Konovalov Nikolay <konovalov.nk@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

global $CFG;
require_once("{$CFG->libdir}/formslib.php");

/*
 * Класс для отображения форм комментариев
 * 
 * В качестве параметров конструктору требуется массив:
      array('commentid' => номер комментария,
            'messageid' => номер сообщения,
            'message'   => текст,
            'author'    => автор,
            'datetime'  => дата поста,
            'buttons'   => функциональные кнопки (редактировать/удалить));
 * Экземпляр формы создаётся таким образом:
 * $form = new form_comment(null, array(...));
 * 
 * @package    block_moodlebook
 * @category   block
 * @copyright  2013 Nikolay Konovalov
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class moodlebook_commentform extends moodleform {
    
    function definition() {
        
        $mform     =& $this->_form;
        $commentid = $this->_customdata['commentid'];
        $messageid = $this->_customdata['messageid'];
        $text      = $this->_customdata['message'];
        $author    = $this->_customdata['author'];
        $datetime  = $this->_customdata['datetime'];
        $mform->addElement('header','cmtheader', get_string('commentary', 'block_moodlebook') . $this->_customdata['commentid']);
        
        $mform->addElement('static', 'author', get_string('author', 'block_moodlebook'), $author);

        $mform->addElement('static', 'messagearea', get_string('messagearea', 'block_moodlebook'), $text);
        $mform->addElement('static', 'date', get_string('date', 'block_moodlebook'), userdate($datetime));
        $mform->setType('messagearea', PARAM_NOTAGS);
        if ($this->_customdata['buttons']) {
            $btnarray = array();
            $btnarray[] =& $mform->createElement('submit', 'editcmt', get_string('buttonedit', 'block_moodlebook'));
            $btnarray[] =& $mform->createElement('submit', 'deletecmt', get_string('buttondelete', 'block_moodlebook'));
            $mform->addGroup($btnarray, 'btnarr', get_string('actions', 'block_moodlebook'), array(' '), FALSE);
        }
        $mform->addElement('hidden', 'msgid', $messageid);
        $mform->setType('msgid', PARAM_INT);
        $mform->addElement('hidden', 'cmtid', $commentid);
        $mform->setType('cmtid', PARAM_INT);
    }
}

/*
 * Класс для отображения формы сообщений
 * 
 * В качестве параметров конструктору требуется массив:
      array('messageid' => номер сообщения,
            'message'   => текст,
            'author'    => автор,
            'datetime'  => дата поста,
            'buttons'   => функциональные кнопки (редактировать/удалить));
 * Экземпляр формы создаётся таким образом:
 * $form = new form_comment(null, array(...));

 * @package    block_moodlebook
 * @category   block
 * @copyright  2013 Nikolay Konovalov
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class moodlebook_messageform extends moodleform {
 
    function definition() {
 
        $mform =& $this->_form;
        $mform->addElement('header','msgheader', get_string('message', 'block_moodlebook') . $this->_customdata['messageid']);
        
        $mform->addElement('static', 'author', get_string('author', 'block_moodlebook'), $this->_customdata['author']);

        $mform->addElement('static', 'messagearea', get_string('messagearea', 'block_moodlebook'), $this->_customdata['message']);
        $mform->addElement('static', 'date', get_string('date', 'block_moodlebook'), userdate($this->_customdata['datetime']));
        $mform->setType('messagearea', PARAM_NOTAGS);
        if ($this->_customdata['buttons']) {
            $btnarray = array();
            $btnarray[] =& $mform->createElement('submit', 'commentmsg', get_string('buttoncomment', 'block_moodlebook'));
            $btnarray[] =& $mform->createElement('submit', 'editmsg', get_string('buttonedit', 'block_moodlebook'));
            $btnarray[] =& $mform->createElement('submit', 'deletemsg', get_string('buttondelete', 'block_moodlebook'));
            $mform->addGroup($btnarray, 'btnarr', get_string('actions', 'block_moodlebook'), array(' '), FALSE);
        }
        $mform->addElement('hidden', 'msgid', $this->_customdata['messageid']);
        $mform->setType('msgid', PARAM_INT);
    }
}

/*
 * Класс для отображения формы отправки сообщений/комментариев/редактирования
 * 
 * В качестве параметров конструктору требуется массив:
      array('messageid' => номер сообщения,
            'commentid' => номер комментария,
            'message'   => текст,
            'action'    => выполняемое действие (comment/editmsg/editcmt/submitbutton)
            'author'    => автор,
            'datetime'  => дата поста);
 * Экземпляр формы создаётся таким образом:
 * $form = new form_comment(null, array(...));
 * 
 * @package    block_moodlebook
 * @category   block
 * @copyright  2013 Nikolay Konovalov
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class moodlebook_submitform extends moodleform {

    function definition() {

        $mform =& $this->_form;
        $action = $this->_customdata['action'];
        $messageid = $this->_customdata['messageid'];
        $commentid = $this->_customdata['commentid'];
        
        $headmessage = '';
        $strings = array('comment'      => 'commentingmsg',
                         'editmsg'      => 'editingmsg',
                         'editcmt'      => 'editingcmt',
                         'submitbutton' => 'sendmsg');
        if ( key_exists($action, $strings) ) {
            $headmessage .= get_string($strings[$action], 'block_moodlebook');
        }
        switch ($action) {
            case 'comment':
            case 'editmsg':
                $headmessage .= $messageid;
                break;
            
            case 'editcmt':
                $headmessage .= $commentid. ' ' . get_string('tomsgnumber', 'block_moodlebook') . $messageid;
                break;

            default:
                break;
        }
        $mform->addElement('header','displayinfo', $headmessage);

        // add author element.
        $mform->addElement('text', 'author', get_string('author', 'block_moodlebook'));
        $mform->addRule('author', null, 'required', null, 'client');
        $mform->setType('author', PARAM_NOTAGS);
        $mform->setConstant('author', $this->_customdata['author']);

        // add display text field
        $mform->addElement('textarea', 'messagearea', get_string('messagearea', 'block_moodlebook'));
        $mform->setType('messagearea', PARAM_NOTAGS);
        $mform->addRule('messagearea', null, 'required', null, 'client');
        $mform->setConstant('messagearea', $this->_customdata['message']);
        $mform->addElement('hidden', 'action', $action);
        $mform->setType('action', PARAM_NOTAGS);

        if (in_array($action, array('comment', 'editmsg', 'editcmt'))) {
            $mform->addElement('hidden', 'msgid', $messageid);
            $mform->setType('msgid', PARAM_INT);
            if ($action == 'editcmt') {
                $mform->addElement('hidden', 'cmtid', $commentid);
                $mform->setType('cmtid', PARAM_INT);
            }
        }

        if ($action == 'submitbutton') {
            $mform->addElement('reset', 'resettext', get_string('buttonreset', 'block_moodlebook'));
            $this->add_action_buttons();
        } else {

            $btnarray = array();
            $actions = array('comment' => 'buttoncomment',
                             'editmsg' => 'buttonedit',
                             'editcmt' => 'buttonedit');
            if (key_exists($action, $actions)) {
                $btnarray[] =& $mform->createElement(
                        'submit',
                        $action,
                        get_string($actions[$action], 'block_moodlebook'));
            }
            $btnarray[] =& $mform->createElement(
                            'reset',
                            'resettext',
                            get_string('buttonreset', 'block_moodlebook'));
            $mform->addGroup(
                            $btnarray,
                            'btnarr',
                            get_string('actions', 'block_moodlebook'),
                            array(' '),
                            FALSE);
        }
    }
}
?>